package http

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"mall-portalv2/global"
	"mall-portalv2/internal/model"
	"mall-portalv2/internal/server/ctx"
	"mall-portalv2/library/common"
	"net/http"
	"time"
)

const (
	TempTestUserID = 1
)

func CartAddHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}

	uidStr, isUid := c.Get("uid")
	if !isUid {
		global.Log.Error("uid不存在")
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, nil)
		return
	}
	UID := common.Str2Int64(fmt.Sprintf("%d", uidStr))

	type RequestBody struct {
		CreateDate        time.Time `json:"createDate"`
		DeleteStatus      int       `json:"deleteStatus"`
		Id                int64     `json:"id"`
		MemberId          int64     `json:"memberId"`
		MemberNickname    string    `json:"memberNickname"`
		ModifyDate        time.Time `json:"modifyDate"`
		Price             float64   `json:"price"`
		ProductAttr       string    `json:"productAttr"`
		ProductBrand      string    `json:"productBrand"`
		ProductCategoryId int64     `json:"productCategoryId"`
		ProductId         int64     `json:"productId"`
		ProductName       string    `json:"productName"`
		ProductPic        string    `json:"productPic"`
		ProductSkuCode    string    `json:"productSkuCode"`
		ProductSkuId      int64     `json:"productSkuId"`
		ProductSn         string    `json:"productSn"`
		ProductSubTitle   string    `json:"productSubTitle"`
		Quantity          int64     `json:"quantity"`
	}

	requestBody := RequestBody{}

	if err := common.BindAndCheck(c, &requestBody); err != nil {
		global.Log.Error("请求参数不正确", zap.Error(err))
		app.Response(http.StatusBadRequest, ctx.StatusBadRequest, err)
		return
	}

	global.Log.Info("映射完成", zap.Any("data", requestBody))

	insertBody := model.OmsCartItem{
		ProductId:         requestBody.ProductId,
		ProductSkuId:      requestBody.ProductSkuId,
		MemberId:          UID,
		Quantity:          requestBody.Quantity,
		Price:             requestBody.Price,
		ProductPic:        requestBody.ProductPic,
		ProductName:       requestBody.ProductName,
		ProductSubTitle:   requestBody.ProductSubTitle,
		ProductSkuCode:    requestBody.ProductSkuCode,
		MemberNickname:    requestBody.MemberNickname,
		CreateDate:        time.Now(),
		ModifyDate:        time.Now(),
		DeleteStatus:      0,
		ProductCategoryId: requestBody.ProductCategoryId,
		ProductBrand:      requestBody.ProductBrand,
		ProductSn:         requestBody.ProductSn,
		ProductAttr:       requestBody.ProductAttr,
	}

	result, err := svc.CreateCart(c, insertBody)
	if err != nil {
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, err)
		return
	}

	app.Response(http.StatusOK, ctx.OK, result)
}

func CartClearHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}

	uidStr, isUid := c.Get("uid")
	if !isUid {
		global.Log.Error("uid不存在")
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, nil)
		return
	}
	UID := common.Str2Int64(fmt.Sprintf("%d", uidStr))

	if err := svc.ClearCart(c, UID); err != nil {
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, err)
		return
	}

	app.Response(http.StatusOK, ctx.OK, nil)
}

func CartDeleteHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}

	uidStr, isUid := c.Get("uid")
	if !isUid {
		global.Log.Error("uid不存在")
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, nil)
		return
	}
	UID := common.Str2Int64(fmt.Sprintf("%d", uidStr))

	type RequestBody struct {
		Ids []int64 `json:"ids"`
	}

	requestBody := RequestBody{}

	if err := common.BindAndCheck(c, &requestBody); err != nil {
		global.Log.Error("请求参数不正确", zap.Error(err))
		app.Response(http.StatusBadRequest, ctx.StatusBadRequest, err)
		return
	}

	global.Log.Info("映射完成", zap.Any("data", requestBody))

	for _, id := range requestBody.Ids {
		if err := svc.DeleteCart(c, UID, id); err != nil {
			app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, err)
			break
		}
	}

	app.Response(http.StatusOK, ctx.OK, nil)
}

func CartGetProdHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}

	productId := common.Str2Int64(c.Param("productId"))

	global.Log.Info("打印接口", zap.Any("id", productId))

	app.Response(http.StatusOK, ctx.OK, nil)
}

func CartListHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}

	uidStr, isUid := c.Get("uid")
	if !isUid {
		global.Log.Error("uid不存在")
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, nil)
		return
	}
	UID := common.Str2Int64(fmt.Sprintf("%d", uidStr))

	result, err := svc.QueryAllCart(c, UID)
	if err != nil {
		app.Response(http.StatusInternalServerError, ctx.StatusInternalServerError, err)
		return
	}

	app.Response(http.StatusOK, ctx.OK, result)
}

func CartUpdateAttrHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}
	app.Response(http.StatusOK, ctx.OK, nil)
}

func CartUpdateQuantityHandlerFunc(c *gin.Context) {
	app := ctx.Gin{C: c}
	app.Response(http.StatusOK, ctx.OK, nil)
}
